home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / e / amigae33a.lha / E_v3.3a / Src.lha / Src / OOmodules / coordinate.e < prev    next >
Text File  |  1995-11-04  |  15KB  |  735 lines

  1. /*
  2.  
  3. rotating
  4.  
  5. seems to work
  6. */
  7.  
  8. OPT MODULE
  9. OPT EXPORT
  10.  
  11. MODULE 'oomodules/sort/numbers/float','oomodules/object'
  12.  
  13. OBJECT coordinate OF object
  14. /****** coordinate/--coordinate-- ******************************************
  15.  
  16.     NAME 
  17.       coordinate of object
  18.  
  19.     PURPOSE
  20.       Unfinished object for three dimensional coordinates.
  21.  
  22. ******************************************************************************
  23.  
  24. History
  25.  
  26.  
  27. */
  28.   x:PTR TO float,
  29.   y:PTR TO float,
  30.   z:PTR TO float
  31. ENDOBJECT
  32.  
  33. PROC name() OF coordinate IS 'coordinate'
  34. /****** coordinate/name ******************************************
  35.  
  36.     NAME 
  37.         name() -- Get name of object.
  38.  
  39.     SYNOPSIS
  40.         coordinate.name()
  41.  
  42.     FUNCTION
  43.         Returns 'Coordinate'
  44.  
  45.     RESULTS
  46.         see above
  47. ******************************************************************************
  48.  
  49. History
  50.  
  51.  
  52. */
  53.  
  54. PROC init() OF coordinate
  55. /****** coordinate/init ******************************************
  56.  
  57.     NAME 
  58.         init() -- Initialization of the object.
  59.  
  60.     SYNOPSIS
  61.         coordinate.init()
  62.  
  63.     FUNCTION
  64.         Initializes the floats in the object.
  65.  
  66.     SEE ALSO
  67.         float/new()
  68. ******************************************************************************
  69.  
  70. History
  71.  
  72.  
  73. */
  74. DEF float_x:PTR TO float,
  75.     float_y:PTR TO float,
  76.     float_z:PTR TO float
  77.  
  78.   NEW float_x.new()
  79.   self.x := float_x
  80.  
  81.   NEW float_y.new()
  82.   self.y := float_y
  83.  
  84.   NEW float_z.new()
  85.   self.z := float_z
  86.  
  87. ENDPROC
  88.  
  89.  
  90. PROC end() OF coordinate
  91. /****** coordinate/end ******************************************
  92.  
  93.     NAME 
  94.         end() -- Destructor.
  95.  
  96.     SYNOPSIS
  97.         coordinate.end()
  98.  
  99.     FUNCTION
  100.         ENDs the Floats.
  101.  
  102.     SEE ALSO
  103.         float/end()
  104. ******************************************************************************
  105.  
  106. History
  107.  
  108.  
  109. */
  110. DEF float:PTR TO float
  111.  
  112.   float := self.x
  113.   END float
  114.  
  115.   float := self.y
  116.   END float
  117.  
  118.   float := self.y
  119.   END float
  120.  
  121. ENDPROC
  122.  
  123.  
  124. PROC getX() OF coordinate IS self.x.get()
  125. /****** coordinate/getX ******************************************
  126.  
  127.     NAME 
  128.         getX() -- Get x part of coordinate.
  129.  
  130.     SYNOPSIS
  131.         coordinate.getX()
  132.  
  133.     FUNCTION
  134.         Returns the value of the x Float of the coordinate.
  135.  
  136.     RESULT
  137.         see float/get()
  138. ******************************************************************************
  139.  
  140. History
  141.  
  142.  
  143. */
  144. PROC getY() OF coordinate IS self.y.get()
  145. /****** coordinate/getY ******************************************
  146.  
  147.     NAME 
  148.         getY() -- Get y part of coordinate.
  149.  
  150.     SYNOPSIS
  151.         coordinate.getY()
  152.  
  153.     FUNCTION
  154.         Returns the value of the y Float of the coordinate.
  155.  
  156.     RESULT
  157.         see float/get()
  158. ****************************************************************************
  159. History
  160.  
  161.  
  162. */
  163. PROC getZ() OF coordinate IS self.z.get()
  164. /****** coordinate/getZ ******************************************
  165.  
  166.     NAME 
  167.         getZ() -- Get z part of coordinate.
  168.  
  169.     SYNOPSIS
  170.         coordinate.getZ()
  171.  
  172.     FUNCTION
  173.         Returns the value of the z Float of the coordinate.
  174.  
  175.     RESULT
  176.         see float/get()
  177. ******************************************************************************
  178.  
  179. History
  180.  
  181.  
  182. */
  183.  
  184. PROC setX(v) OF coordinate IS self.x.set(v)
  185. /****** coordinate/setX ******************************************
  186.  
  187.     NAME 
  188.         setX() -- Set x part of coordinate.
  189.  
  190.     SYNOPSIS
  191.         coordinate.setX(v)
  192.  
  193.     FUNCTION
  194.         Sets the value of the x Float of the coordinate.
  195.  
  196.     INPUTS
  197.         parameter for float/set()
  198. ******************************************************************************
  199.  
  200. History
  201.  
  202.  
  203. */
  204.  
  205. PROC setY(v) OF coordinate IS self.y.set(v)
  206. /****** coordinate/setY ******************************************
  207.  
  208.     NAME 
  209.         setY() -- Set y part of coordinate.
  210.  
  211.     SYNOPSIS
  212.         coordinate.setY(v)
  213.  
  214.     FUNCTION
  215.         Sets the value of the y Float of the coordinate.
  216.  
  217.     INPUTS
  218.         parameter for float/set()
  219. ******************************************************************************
  220.  
  221. History
  222.  
  223.  
  224. */
  225. PROC setZ(v) OF coordinate IS self.z.set(v)
  226. /****** coordinate/setZ ******************************************
  227.  
  228.     NAME 
  229.         setZ() -- Set z part of coordinate.
  230.  
  231.     SYNOPSIS
  232.         coordinate.setZ(v)
  233.  
  234.     FUNCTION
  235.         Sets the value of the z Float of the coordinate.
  236.  
  237.     INPUTS
  238.         parameter for float/set()
  239. ******************************************************************************
  240.  
  241. History
  242.  
  243.  
  244. */
  245.  
  246.  
  247.  
  248. /* returns an array with a normal compare to all three dims?
  249. PROC cmp(item:PTR TO integer) OF integer
  250.  IF self.number < item.number THEN RETURN -1
  251.  RETURN IF self.number > item.number THEN 1 ELSE 0
  252. ENDPROC
  253.  
  254. */
  255.  
  256. PROC select(optionlist,index) OF coordinate
  257. /****** coordinate/select ******************************************
  258.  
  259.     NAME 
  260.         select() -- Selection of action on initialization.
  261.  
  262.     SYNOPSIS
  263.         coordinate.select(optionlist, index)
  264.  
  265.     FUNCTION
  266.         Recognizes the following items:
  267.             "set" -- The following three items have to be of the type
  268.                 that set() expects.
  269.  
  270.     INPUTS
  271.         optionlist -- The optionlist
  272.  
  273.         index -- the index of the optionlist
  274.  
  275.     SEE ALSO
  276.         object/select()
  277. ******************************************************************************
  278.  
  279. History
  280.  
  281.  
  282. */
  283. /*
  284.  
  285. TODO: error check: len-o'-list!
  286.  
  287. */
  288. DEF item, value
  289.  
  290.   item := ListItem(optionlist, index)
  291.  
  292.   SELECT item
  293.     CASE "set"
  294.       INC index
  295.       self.x.set(ListItem(optionlist,index))
  296.  
  297.       INC index
  298.       self.y.set(ListItem(optionlist,index))
  299.  
  300.       INC index
  301.       self.z.set( ListItem(optionlist,index))
  302.  
  303.   ENDSELECT
  304.  
  305. ENDPROC index
  306.  
  307.  
  308. PROC write() OF coordinate
  309. /****** coordinate/write ******************************************
  310.  
  311.     NAME
  312.         write() -- Get string with printable coordinate.
  313.  
  314.     SYNOPSIS
  315.         coordinate.write()
  316.  
  317.     FUNCTION
  318.         Returns a string with the printable coordinate. It looks like this:
  319.  
  320.         [ <x> ; <y> ; <z> ]
  321.  
  322.     RESULT
  323.         String above.
  324.  
  325.     NOTES
  326.         The string has a maximum legth of 100 characters.
  327.  
  328. ******************************************************************************
  329.  
  330. History
  331.  
  332.  
  333. */
  334. DEF out
  335.  
  336.   out:=String(100)
  337.  
  338.   StrAdd(out,'[ ')
  339.   StrAdd(out, self.x.write())
  340.   StrAdd(out,' ; ')
  341.   StrAdd(out, self.y.write())
  342.   StrAdd(out,' ; ')
  343.   StrAdd(out, self.z.write())
  344.   StrAdd(out,' ]')
  345.  
  346.   RETURN out
  347. ENDPROC
  348.  
  349.  
  350. PROC copy(to=NIL:PTR TO coordinate) OF coordinate
  351. /****** coordinate/copy ******************************************
  352.  
  353.     NAME 
  354.         copy() -- Copy a coordinate
  355.  
  356.     SYNOPSIS
  357.         coordinate.copy(destination=NIL)
  358.  
  359.     FUNCTION
  360.         Copies a coordinate.
  361.  
  362.     INPUTS
  363.         destination=NIL:PTR TO coordinate -- Coordinate that is the
  364.             destination of the copy. IF NIL, a new coordinate will be
  365.             created.
  366.  
  367.     RESULT
  368.         PTR TO coordinate -- If the incoming coordinate object was
  369.             NIL a freshly created coordinated will be returned.
  370.  
  371.     NOTES
  372.         May raise extension on NEWing the coordinate when the incoming object
  373.         is NIL.
  374.  
  375. ******************************************************************************
  376.  
  377. History
  378.  
  379.  
  380. */
  381. DEF destination:PTR TO coordinate
  382.  
  383.   IF to=NIL THEN NEW destination.new() ELSE destination := to
  384.  
  385.   self.x.copy( destination.x )
  386.   self.y.copy( destination.y )
  387.   self.z.copy( destination.z )
  388.  
  389. ENDPROC destination
  390.  
  391.  
  392. PROC shift(by:PTR TO coordinate) OF coordinate
  393. /****** coordinate/shift ******************************************
  394.  
  395.     NAME 
  396.         shift() -- Shift a coordinate.
  397.  
  398.     SYNOPSIS
  399.         coordinate.shift(PTR TO coordinate)
  400.  
  401.     FUNCTION
  402.         Shifts a coordinate by another.
  403.  
  404.     INPUTS
  405.         PTR TO coordinate -- Coordinate to shift by.
  406.  
  407.     EXAMPLE
  408.         Be baseCoordinate [ 1.0 ; 0.0 ; 2.0 ]
  409.         Be secondCoordinate [ 0.0 ; 1.0 ; 1.0 ]
  410.  
  411.         baseCoordinate.shift(secondCoordinate) would result in
  412.         baseCoordinate being [ 1.0 ; 1.0 ; 3.0 ]
  413.  
  414. ******************************************************************************
  415.  
  416. History
  417.  
  418.  
  419. */
  420.  
  421.   self.x.add(by.x)
  422.   self.y.add(by.y)
  423.   self.z.add(by.z)
  424.  
  425. ENDPROC
  426.  
  427.  
  428. -> turns 0-360 to 0-2*pi
  429. PROC angle2radians(angle) OF coordinate
  430. /****** coordinate/angle2radians ******************************************
  431.  
  432.     NAME 
  433.         angle2radians() -- Turn angle to radians.
  434.  
  435.     SYNOPSIS
  436.         coordinate.angle2radians(angle)
  437.  
  438.     FUNCTION
  439.         Turns an angle into it's radians equivalent. 360 degree would result
  440.         in 2*PI.
  441.  
  442.     INPUTS
  443.         angle -- Normal angle between 0 and 360 degree. HAS to be an e-float!
  444.  
  445.     RESULT
  446.         radians -- The according radians
  447.  
  448. ******************************************************************************
  449.  
  450. History
  451.  
  452.  
  453. */
  454. /*
  455.  
  456. Seems to work only with angles>0
  457. NOTE: only float angles, please!
  458.  
  459. */
  460. DEF out[80]:STRING
  461.  
  462.   IF angle<0 THEN angle := angle+360
  463.  
  464.   angle := angle/180.0
  465.   angle := angle*3.14159265
  466.  
  467. ->  RealF(out,angle,8)
  468. ->  WriteF('\s\n', out)
  469.  
  470.   RETURN angle
  471. ENDPROC
  472.  
  473.  
  474. PROC rotateZ(angle,coordinate=NIL:PTR TO coordinate) OF coordinate
  475. /****** coordinate/rotateZ ******************************************
  476.  
  477.     NAME 
  478.         rotateZ() -- Rotate around the z axis.
  479.  
  480.     SYNOPSIS
  481.         coordinate.rotateZ(e-float,PTR TO coordinate)
  482.  
  483.     FUNCTION
  484.         If the second parameter is NIL the coordinate is rotated around the
  485.         z axis by the given angle. If a coordinate is specified, however,
  486.         the object will be rotated around the z axis that goes through that
  487.         point.
  488.  
  489.     INPUTS
  490.         angle -- Angle to rotate by. HAS to be an e-float!
  491.  
  492.         coordinate=NIL:PTR TO coordinate -- If provided the object won't be
  493.             rotated around [ 0.0 ; 0.0 ; 0.0 ] but around this coordinate.
  494.  
  495.     SEE ALSO
  496.         rotateY(), rotateX()
  497. ******************************************************************************
  498.  
  499. History
  500.  
  501.  
  502. */
  503. /*
  504.  
  505. This method rotates the point by the given angle around the given coordinate.
  506. If no coordinate is specified, we rotate aroung 0;0;0
  507.  
  508. */
  509.  
  510. DEF fltangle, nux, nuy, out[80]:STRING,
  511.     fltx,flty, resulta, resultb
  512.  
  513.  /*
  514.   * To rotate around a point we do this: substract the coordinate's values,
  515.   * i.e. shift it to 0;0;0, rotate it, and add the coordinate's values
  516.   * to the result.
  517.   */
  518.  
  519.   IF coordinate
  520.     self.x.substract(coordinate.x)
  521.     self.y.substract(coordinate.y)
  522.     self.z.substract(coordinate.z)
  523.   ENDIF
  524.  
  525.  
  526.   fltangle := self.angle2radians(angle)
  527.  
  528.   fltx := self.x.get()
  529.   flty := self.y.get()
  530.  
  531.   resulta := !fltx * Fcos(fltangle)
  532.   resultb := !flty * Fsin(fltangle)
  533.  
  534.   nux := !resulta - resultb
  535.  
  536.   resulta := !fltx * Fsin(fltangle)
  537.   resultb := !flty * Fcos(fltangle)
  538.  
  539.   nuy := !resulta + resultb
  540.  
  541.  /*
  542.   * Now let's shift it back to where it came from (see above)
  543.   */
  544.  
  545.   self.x.set(nux)
  546.   self.y.set(nuy)
  547.  
  548.   IF coordinate
  549.     self.x.add(coordinate.x)
  550.     self.y.add(coordinate.y)
  551.     self.z.add(coordinate.z)
  552.   ENDIF
  553.  
  554. ENDPROC
  555.  
  556.  
  557. PROC rotateY(angle,coordinate=NIL:PTR TO coordinate) OF coordinate
  558. /****** coordinate/rotateY ******************************************
  559.  
  560.     NAME 
  561.         rotateY() -- Rotate around the y axis.
  562.  
  563.     SYNOPSIS
  564.         coordinate.rotateY(e-float,PTR TO coordinate)
  565.  
  566.     FUNCTION
  567.         If the second parameter is NIL the coordinate is rotated around the
  568.         y axis by the given angle. If a coordinate is specified, however,
  569.         the object will be rotated around the y axis that goes through that
  570.         point.
  571.  
  572.     INPUTS
  573.         angle -- Angle to rotate by. HAS to be an e-float!
  574.  
  575.         coordinate=NIL:PTR TO coordinate -- If provided the object won't be
  576.             rotated around [ 0.0 ; 0.0 ; 0.0 ] but around this coordinate.
  577.  
  578.     SEE ALSO
  579.         rotateZ(), rotateX()
  580. ******************************************************************************
  581.  
  582. History
  583.  
  584.  
  585. */
  586. /*
  587.  
  588.   note:
  589.  
  590.   to rotate 90 degree (mathematical) one has to provide -90
  591.   (wrong formula?)
  592. */
  593.  
  594. DEF fltangle, nux, nuz, out[80]:STRING,
  595.     fltx,fltz, resulta, resultb
  596.  
  597.  
  598.   IF coordinate
  599.     self.x.substract(coordinate.x)
  600.     self.y.substract(coordinate.y)
  601.     self.z.substract(coordinate.z)
  602.   ENDIF
  603.  
  604.  
  605.   fltangle := self.angle2radians(angle)
  606.  
  607.   fltx := self.x.get()
  608.   fltz := self.z.get()
  609.  
  610.   resulta := !fltz * Fcos(fltangle)
  611.   resultb := !fltx * Fsin(fltangle)
  612.  
  613.   nuz := !resulta - resultb
  614.  
  615.   resulta := !fltz * Fsin(fltangle)
  616.   resultb := !fltx * Fcos(fltangle)
  617.  
  618.   nux := !resulta + resultb
  619.  
  620.   self.x.set(nux)
  621.   self.z.set(nuz)
  622.  
  623.   IF coordinate
  624.     self.x.add(coordinate.x)
  625.     self.y.add(coordinate.y)
  626.     self.z.add(coordinate.z)
  627.   ENDIF
  628.  
  629. ENDPROC
  630.  
  631.  
  632. PROC rotateX(angle,coordinate=NIL:PTR TO coordinate) OF coordinate
  633. /****** coordinate/rotateX ******************************************
  634.  
  635.     NAME 
  636.         rotateX() -- Rotate around the x axis.
  637.  
  638.     SYNOPSIS
  639.         coordinate.rotateX(e-float,PTR TO coordinate)
  640.  
  641.     FUNCTION
  642.         If the second parameter is NIL the coordinate is rotated around the
  643.         x axis by the given angle. If a coordinate is specified, however,
  644.         the object will be rotated around the x axis that goes through that
  645.         point.
  646.  
  647.     INPUTS
  648.         angle -- Angle to rotate by. HAS to be an e-float!
  649.  
  650.         coordinate=NIL:PTR TO coordinate -- If provided the object won't be
  651.             rotated around [ 0.0 ; 0.0 ; 0.0 ] but around this coordinate.
  652.  
  653.     SEE ALSO
  654.         rotateY(), rotateZ()
  655. ******************************************************************************
  656.  
  657. History
  658.  
  659.  
  660. */
  661. /*
  662.  
  663.   note:
  664.  
  665.   to rotate 90 degree (mathematical) one has to provide -90
  666.   (wrong formula?)
  667. */
  668.  
  669. DEF fltangle, nuy, nuz, out[80]:STRING,
  670.     fltz,flty, resulta, resultb
  671.  
  672.   IF coordinate
  673.     self.x.substract(coordinate.x)
  674.     self.y.substract(coordinate.y)
  675.     self.z.substract(coordinate.z)
  676.   ENDIF
  677.  
  678.  
  679.   fltangle := self.angle2radians(angle)
  680.  
  681.   fltz := self.z.get()
  682.   flty := self.y.get()
  683.  
  684.   resulta := !flty * Fcos(fltangle)
  685.   resultb := !fltz * Fsin(fltangle)
  686.  
  687.   nuy := !resulta - resultb
  688.  
  689.   resulta := !flty * Fsin(fltangle)
  690.   resultb := !fltz * Fcos(fltangle)
  691.  
  692.   nuz := !resulta + resultb
  693.  
  694.   self.y.set(nuy)
  695.   self.z.set(nuz)
  696.  
  697.   IF coordinate
  698.     self.x.add(coordinate.x)
  699.     self.y.add(coordinate.y)
  700.     self.z.add(coordinate.z)
  701.   ENDIF
  702.  
  703. ENDPROC
  704.  
  705.  
  706. PROC neg() OF coordinate
  707. /****** cordinate/neg ******************************************
  708.  
  709.     NAME 
  710.         neg() -- Negate cordinate.
  711.  
  712.     SYNOPSIS
  713.         coordinate.neg()
  714.  
  715.     FUNCTION
  716.         Negates the x, y and z value of the coordinate.
  717.  
  718.     SEE ALSO
  719.         float/neg()
  720. ******************************************************************************
  721.  
  722. History
  723.  
  724.  
  725. */
  726.   self.x.neg()
  727.   self.y.neg()
  728.   self.z.neg()
  729. ENDPROC
  730.  
  731. /*EE folds
  732. 1
  733. 54 34 56 32 190 50 192 40 194 40 196 34 199 43 201 81 203 73 205 72 207 24 
  734. EE folds*/
  735.